﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows.Forms;
using SoundEdge.Media.Wave;
using SoundEdge.Media.Wave.SampleProviders;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.IO;
using SoundEdge.Media.Settings;
using SoundEdge.Common;

namespace SoundEdge.WinControls.Settings
{
    [Export]
    public partial class OutputDeviceSettingsPanel : UserControl
    {
        [ImportMany(typeof(IInputFileFormatPlugin))]
        public IEnumerable<IInputFileFormatPlugin> InputFileFormats { get; set; }

        [ImportMany(typeof(IOutputDevicePlugin))]
        public IEnumerable<IOutputDevicePlugin> OutputDevicePlugins { get; set; }

        public IOutputSettings Settings
        {
            get { return SelectedOutputDevicePlugin != null ? SelectedOutputDevicePlugin.Settings : null; }
        }

        public OutputDeviceSettingsPanel()
        {
            InitializeComponent();

            MEFUtils.GetAssemblyContainer().ComposeParts(this);
            LoadOutputDevicePlugins();
        }
    
        private void LoadOutputDevicePlugins()//IEnumerable<IOutputDevicePlugin> outputDevicePlugins)
        {
            if (OutputDevicePlugins == null)
            {
                return;
            }
            comboBoxOutputDevice.DisplayMember = "Name";
            comboBoxOutputDevice.SelectedIndexChanged += new EventHandler(comboBoxOutputDevice_SelectedIndexChanged);
            foreach (var outputDevicePlugin in OutputDevicePlugins.OrderBy(p => p.Priority))
            {
                comboBoxOutputDevice.Items.Add(outputDevicePlugin);
            }
            IOutputDevicePlugin selected = comboBoxOutputDevice.Items.OfType<IOutputDevicePlugin>().FirstOrDefault(item => item.Settings != null && !string.IsNullOrEmpty(item.Settings.DeviceName));
            if (selected != null)
            {
                comboBoxOutputDevice.SelectedIndex = comboBoxOutputDevice.Items.IndexOf(selected);
            }
            else
            {
                comboBoxOutputDevice.SelectedIndex = 0;
            }
        }

        void comboBoxOutputDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Settings != null)
            {
                Settings.DeviceName = SelectedOutputDevicePlugin.Name;
            }
            panelOutputDeviceSettings.Controls.Clear();
            Control settingsPanel;
            if (SelectedOutputDevicePlugin.IsAvailable)
            {
                settingsPanel = SelectedOutputDevicePlugin.CreateSettingsPanel();
            }
            else
            {
                settingsPanel = new Label() { Text = "This output device is unavailable on your system", Dock = DockStyle.Fill };
            }
            panelOutputDeviceSettings.Controls.Add(settingsPanel);
        }

        private IOutputDevicePlugin SelectedOutputDevicePlugin
        {
            get { return (IOutputDevicePlugin)comboBoxOutputDevice.SelectedItem; }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBoxLatency.Items.Add(25);
            comboBoxLatency.Items.Add(50);
            comboBoxLatency.Items.Add(100);
            comboBoxLatency.Items.Add(150);
            comboBoxLatency.Items.Add(200);
            comboBoxLatency.Items.Add(300);
            comboBoxLatency.Items.Add(400);
            comboBoxLatency.Items.Add(500);

            if (Settings != null)
            {
                int? selected = comboBoxLatency.Items.OfType<int>().FirstOrDefault(item => item == Settings.Latency);
                int index = selected.HasValue ? comboBoxLatency.Items.IndexOf(selected) : 5;
                comboBoxLatency.SelectedIndex = index >= 0 ? index : 5;
            }
            else
            {
                comboBoxLatency.SelectedIndex = 5;
            }
        }

        private void comboBoxLatency_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Settings != null)
            {
                Settings.Latency = (int)comboBoxLatency.SelectedItem;
            }
        }
    }
}

